package com.zimi.controller;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zimi.model.*;
import com.zimi.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api("活动管理")
@Controller
@RequestMapping("wf/activity")
public class WfActivityApi {

    @Autowired
    private WfActivityService service;
    @Autowired
    private WfActivityAuthorityService wfActivityAuthorityService;
    @Autowired
    private WfActivityLineService wfActivityLineService;
    @Autowired
    private WfSystaskService wfSystaskService;
    @Autowired
    private WfSystaskKeyService wfSystaskKeyService;

    /*@PostMapping("list")
    @ResponseBody
    @ApiOperation("查询所有")
    public List<WfActivity> selectAll(WfActivity record) {
        return service.selectAll(record);
    }*/
    @PostMapping("list")
    @ResponseBody
    @ApiOperation("查询所有")
    public Object list(WfActivity record) {
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("List", service.selectAll(record));
        objectMap.put("OK", "OK");
        objectMap.put("code", 200);
        return objectMap;
    }

    @PostMapping("{id}")
    @ResponseBody
    @ApiOperation("根据Id查询信息")
    public Object selectById(@PathVariable("id") int id) {
        Map<String, Object> objectMap = new HashMap<>();
        try {
            objectMap.put("wfActivity", service.selectByPrimaryKey(id));
        } catch (Exception e) {
            objectMap.put("OK", "error");
            objectMap.put("code", "400");
            objectMap.put("message", e.getMessage());
        }
        objectMap.put("OK", "OK");
        objectMap.put("code", "400");
        return objectMap;
    }

    @DeleteMapping("{id}")
    @ResponseBody
    @ApiOperation("根据Id删除")
    public boolean deleteByPrimaryKey(@PathVariable("id") int id) {
        return service.deleteByPrimaryKey(id) > 0 ? true : false;
    }

    @PostMapping("record")
    @ResponseBody
    @ApiOperation("添加")
    public boolean insert(WfActivity record) {
        return service.insertSelective(record) > 0 ? true : false;
    }

    @PostMapping("update")
    @ResponseBody
    @ApiOperation("根据条件修改信息")
    public Object updateByPrimaryKey(WfActivity record) {
        Map<String, Object> objectMap = new HashMap<>();
        try {
            service.updateByPrimaryKeySelective(record);
        } catch (Exception e) {
            e.printStackTrace();
            objectMap.put("OK", "error");
            objectMap.put("code", 400);
            objectMap.put("message", e.getMessage());
        }
        objectMap.put("OK", "OK");
        return objectMap;
    }

    @PostMapping("createActivity")
    @ResponseBody
    @ApiOperation("新建活动")
    public Object createActivity(WfActivity record) {
        Map<String, Object> objectMap = new HashMap<>();
        try {
            objectMap.put("wfActivity", service.saveNewActivity(record));
        } catch (Exception e) {
            objectMap.put("OK", "error");
            objectMap.put("code", 400);
            objectMap.put("message", e.getMessage());
        }
        objectMap.put("code", 200);
        objectMap.put("OK", "OK");
        return objectMap;
    }

    @PostMapping("listVO")
    @ResponseBody
    @ApiOperation("查询所有")
    public Object listVO(WfActivityVO record) {
        Map<String, Object> objectMap = new HashMap<>();
        try {
            objectMap.put("List", service.selectAll(record));
        } catch (Exception e) {
            objectMap.put("Ok", "error");
            objectMap.put("code", 400);
            return objectMap;
        }
        objectMap.put("OK", "OK");
        objectMap.put("code", 200);
        return objectMap;
    }


    @PostMapping("listAllByWfActivityNoStartEnd")
    @ResponseBody
    @ApiOperation("根据流程查询除开始和结束的环节")
    public Object listAllByWfActivityNoStartEnd(WfActivity record) {
        Map<String, Object> objectMap = new HashMap<>();
        try {
            objectMap.put("rows", service.listAllByWfActivityNoStartEnd(record));
        } catch (Exception e) {
            objectMap.put("Ok", "error");
            objectMap.put("code", 400);
            return objectMap;
        }
        objectMap.put("OK", "OK");
        objectMap.put("code", 200);
        return objectMap;
    }


    @PostMapping("saveActivity")
    @ResponseBody
    @ApiOperation("查询所有")
    public Object saveActivity(String reqData) {
        Map<String, Object> objectMap = new HashMap<>();
        WfActivityAndAuthority nodeData = null;
        Gson gson = new Gson();
        try {
            nodeData = gson.fromJson(reqData, new TypeToken<WfActivityAndAuthority>() {
            }.getType());
        } catch (Exception e) {
            objectMap.put("Ok", "error");
            objectMap.put("code", 400);
            objectMap.put("message", e.getMessage());
            return objectMap;
        }
        try {
            WfActivity wfActivity = nodeData.getWfActivity();
            if (wfActivity.getWfId() == null || "".equals(wfActivity.getWfId())) {
                objectMap.put("OK", "error");
                objectMap.put("message", "提交参数不全，缺少wfCode，请检查！");
                return objectMap;
            }

            if (wfActivity.getUpdateTime() == null) {
                wfActivity.setUpdateTime(new Date());
            }
            if (wfActivity.getWfId() == null || wfActivity.getWfId().intValue() == 0) {
                service.insertSelective(wfActivity);
            } else {
                service.updateByPrimaryKeySelective(wfActivity);
            }
            if (nodeData.getWfActivityAuthorityList().size() > 0) {
                // 删除以前的数据
                wfActivityAuthorityService.deleteByActivityId(wfActivity.getWfaId());
                //保存新的数据
                wfActivityAuthorityService.saveBatch(nodeData.getWfActivityAuthorityList());
            }
        } catch (Exception e) {
            e.printStackTrace();
            objectMap.put("OK", "error");
            objectMap.put("code", 400);
            objectMap.put("message", e.getMessage());
            return objectMap;
        }
        objectMap.put("OK", "OK");
        objectMap.put("code", 200);
        return objectMap;
    }

    @PostMapping("delActivity")
    @ResponseBody
    @ApiOperation("删除活动节点和该节点有关的连线")
    public Object delActivity(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            service.deleteByPrimaryKey(id);//删除活动数据
            WfActivityLineVO wfActivityLineVO = new WfActivityLineVO();
            wfActivityLineVO.setSourceId(id);
            wfActivityLineService.deleteByWfId(wfActivityLineVO);//删除连线数据
        } catch (Exception e) {
            map.put("OK", "error");
            map.put("code", 400);
            map.put("message", e.getMessage());
        }
        map.put("OK", "OK");
        map.put("code", 200);
        return map;
    }

    @PostMapping("wfnull")
    @ResponseBody
    @ApiOperation("得到所有节点（当前环节和开始环节除外）")
    public Object selectWfNull(Integer id, WfActivity record) {
        Map<String, Object> objectMap = new HashMap<>();
        try {
            List<WfActivity> list = service.selectWfNull(record);
            if (id == null) {
                //去重
                for (int i = 0; i < list.size() - 1; i++) {
                    for (int j = list.size() - 1; j > i; j--) {
                        if (list.get(j).getActivityCode().equals(list.get(i).getActivityCode())) {
                            list.remove(j);
                        }
                    }
                }
            } else {
                WfSystask wfSystask = wfSystaskService.selectByPrimaryKey(id);
                List<WfActivity> wfActivitylist = new ArrayList<>();
                for (WfActivity wfActivity : list) {
                    if ("end".equals(wfActivity.getActivityCode())) continue;
                    List<SysUser> sysUserList = wfSystaskKeyService.getActivityOperator(wfSystask, wfActivity.getWfaId());
                    if (sysUserList.size() == 0 || sysUserList == null) {
                        wfActivitylist.add(wfActivity);
                    }
                }
                for (WfActivity wfActivity : wfActivitylist) {
                    list.remove(wfActivity);
                }
            }
            objectMap.put("count", list.size());
            objectMap.put("List", list);
        } catch (Exception e) {
            e.printStackTrace();
            objectMap.put("OK", "error");
            objectMap.put("code", 400);
            objectMap.put("message", e.getMessage());
        }
        objectMap.put("OK", "OK");
        objectMap.put("code", 200);
        return objectMap;
    }


}
